package org.shopping.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 处理全局的post请求的中文乱码问题
 */
@WebFilter(urlPatterns = "/*")
public class FilterEncoding implements Filter {
    public void destroy(){
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException{
       //1.将请求对象转成带http协议的请求对象
        HttpServletRequest request = (HttpServletRequest) req;
        //2.判断页面提交的请求方式是post
        if(request.getMethod().equalsIgnoreCase("post")){
           //3.处理全局的post请求中文乱码问题
            request.setCharacterEncoding("utf-8");
        }
        //4.放行时,使用处理请求乱码的请求对象:request
        chain.doFilter(request, resp);
    }

    public void init(FilterConfig config) throws ServletException{

    }

}
